package org.jerrymouse.jsa4j.db.kv.example.dao;

import org.jerrymouse.jsa4j.db.kv.JsonUtil;
import org.jerrymouse.jsa4j.db.kv.Repository;
import org.jerrymouse.jsa4j.db.kv.example.entity.User;

public class SimpleUserDao {
	static JsonUtil<User> jsonUtil = new JsonUtil<User>();
	static String USER = "user";

	public void deleteUser(User user) {
		getRepository().put(user.getId(), null);
	}

	private Repository getRepository() {
		Repository repository = RepositoryFactory.getRepository(USER);
		return repository;
	}

	public User getUser(String key) {
		User user = jsonUtil.toBean(getRepository().get(key), User.class);
		if (user != null)
			user.setId(key);
		return user;
	}

	public User insertUser(User user) {
		String key = getRepository().put(user.getId(), jsonUtil.toJson(user));
		user.setId(key);
		return user;
	}

	public void updateUser(User user) {
		getRepository().put(user.getId(), jsonUtil.toJson(user));
	}
}
